package com.luoying.itew.manage;

import android.text.TextUtils;

import com.luoying.itew.bean.DistrictBean;
import com.luoying.itew.bean.MonitorBean;
import com.luoying.itew.bean.PointCityBean;
import com.luoying.itew.bean.StreetBean;
import com.luoying.itew.bean.UserInfo;
import com.luoying.itew.common.Constant;
import com.luoying.itew.db.DBManager;
import com.luoying.itew.db.DBThreadPool;
import com.luoying.itew.http.rxjava.RxTransformerHelper;
import com.luoying.itew.util.LogUtils;
import com.luoying.itew.util.MonitorUtils;
import com.luoying.itew.util.SharedUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class UserManage {
    private static volatile UserManage instance;
    /**用户token**/
    private String userToekn = "";
    /**用户城市信息*/
    private PointCityBean userCityBean;
    /**用户监测区域的集合*/
    private List<DistrictBean> monitorBeanList = new ArrayList<>();
    /**用户的所有监测点**/
    private List<MonitorBean> allMonitorList = new ArrayList<>();
    /**用户的所有监测点**/
    private List<StreetBean> allStreetList = new ArrayList<>();
    /**
     * 用户信息
     **/
    private UserInfo userInfo;

    private UserManage(){}

    public static UserManage getInstance() {
        if(instance==null){
            synchronized (UserManage.class){
                if(instance==null){
                    instance = new UserManage();
                }
            }
        }
        return instance;
    }

    /**
     * 保存用户城市信息
     * @param userCityBean
     */
    public void saveUserCity(PointCityBean userCityBean){
        this.userCityBean = userCityBean;
    }

    public void initUser(){
        UserInfo tempUser = DBManager.getInstance().getUser();
        userInfo = tempUser;
        Observable.create((ObservableOnSubscribe<List<MonitorBean>>) emitter -> {
            try {
                List<PointCityBean> cityList = DBManager.getInstance().getCityList();
                List<MonitorBean> list = DBManager.getInstance().getPointList();
                for (MonitorBean bean : list) {
                    for (PointCityBean pointCityBean : cityList) {
                        if(bean.getMonitorAreaId()==pointCityBean.getStreetId()){
                            bean.setMonitorArea(pointCityBean);
                            break;
                        }
                    }
                }
                emitter.onNext(list);
                emitter.onComplete();
            } catch (Exception e) {
                e.printStackTrace();
                emitter.onError(e);
            }
        }).compose(RxTransformerHelper.observableIO2Main())
                .subscribe(new Observer<List<MonitorBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        LogUtils.e(d.toString());
                    }

                    @Override
                    public void onNext(List<MonitorBean> list) {
                        allMonitorList.addAll(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public PointCityBean getUserCityBean() {
        return userCityBean;
    }

    public void setMonitorBeanList(List<DistrictBean> list) {
        if(list==null){
            return;
        }
        monitorBeanList.clear();
        monitorBeanList.addAll(list);
    }

    public List<DistrictBean> getMonitorBeanList() {
        return monitorBeanList==null?new ArrayList<DistrictBean>():monitorBeanList;
    }

    /**
     * 获取用户的所有监测点
     * @return
     */
    public List<MonitorBean> getAllMonitorList() {
//        if(getMonitorBeanList().isEmpty()){
//            return allMonitorList;
//        }
//        for (DistrictBean districtBean : getMonitorBeanList()) {
//            for (MonitorBean bean : districtBean.getMonitorPoints()) {
//                //过滤掉未上线的数据
//                if(bean.getMonitorStatus()==1){
//                    allMonitorList.add(bean);
//                }
//            }
//        }
        return allMonitorList;
    }

    /**
     * 保存监测集合
     * @param list
     */
    public void saveMonitorList(List<MonitorBean> list){
        allMonitorList.clear();
        allMonitorList.addAll(list);
        DBThreadPool.getPool().submit(() -> {
            //保存检测点数到数据库
            DBManager.getInstance().savePointList(list);
            List<PointCityBean> cityList = new ArrayList<>(list.size());
            for (MonitorBean bean : list) {
                cityList.add(bean.getMonitorArea());
            }
            //保存城市数据到数据库
            DBManager.getInstance().saveCityList(cityList);
        });
    }

    public MonitorBean getMonitorById(int id){
        MonitorBean bean = null;
        for (MonitorBean monitorBean : getAllMonitorList()) {
            if(monitorBean!=null&&monitorBean.getMonitorPointId()==id){
                bean = monitorBean;
                break;
            }
        }
        return bean;
    }


    /**
     * 获取用户token
     * @return
     */
    public String getUserToekn(){
        if(TextUtils.isEmpty(userToekn)){
            userToekn = SharedUtils.getUserToken();
        }
        return userToekn;
    }

    /**
     * 保存用户token
     * @param userToekn
     */
    public void saveUserToken(String userToekn){
        this.userToekn = userToekn;
        SharedUtils.saveUserToken(userToekn);
    }

    /**
     * 获取用户信息
     * @return
     */
    public UserInfo getUserInfo() {
        if(userInfo==null){
            userInfo = new UserInfo();
        }

        return userInfo;
    }

    /**
     * 保存用户信息
     * @param userInfo
     */
    public void saveUserInfo(UserInfo userInfo) {
        userInfo.setLoginName(SharedUtils.getUserAccount());
        this.userInfo = userInfo;
        DBThreadPool.getPool().submit(() -> DBManager.getInstance().saveUser(userInfo));
    }

    public List<StreetBean> getAllStreetList() {
        return allStreetList==null?new ArrayList<StreetBean>():allStreetList;
    }

    public void saveAllStreet(List<StreetBean> allStreetLIst) {
        this.allStreetList = allStreetLIst;
    }

    /**
     * 依据街道办来查找下面的监测点
     * @param bean
     * @return
     */
    public List<MonitorBean> getListMonitorFromStree(StreetBean bean){
        List<MonitorBean> list = new ArrayList<>();
        //区政府没有监测点
        if(bean==null||bean.getRegionId()==1){
            return list;
        }
        for (MonitorBean monitorBean : getAllMonitorList()) {
            if(monitorBean.getMonitorArea().getAreaId()==bean.getRegionAreaId()){
                list.add(monitorBean);
            }
        }
        return list;
    }

    /**
     * 按区政府查找
     * @param bean
     * @return
     */
    public List<MonitorBean> getListMonitorFromStreeId(StreetBean bean){
        List<MonitorBean> list = new ArrayList<>();
        //区政府没有监测点
        if(bean==null||bean.getRegionId()==1){
            return list;
        }
        for (MonitorBean monitorBean : getAllMonitorList()) {
            if(monitorBean.getMonitorArea().getParentAreaId()==bean.getRegionAreaId()){
                list.add(monitorBean);
            }
        }
        return list;
    }

    /**
     * 获取相同街道办id下的监测点报警状态，取最高级： 报警>预警>正常
     * @param id 街道办ID
     * @return 0正常 1报警 2预警
     */
    public int getStreetState(int id){
        int state = Constant.WARN_LEVEL_GREEN;
        for (MonitorBean bean : getAllMonitorList()) {
            if(bean.getMonitorArea().getAreaId()==id){
                int level = MonitorUtils.getMonitorState(bean);
                if(state<level){
                    state = level;
                }
            }
        }
        return state;
    }

    /**
     * 按预警级别获取监测点集合
     * @return
     */
    public List<MonitorBean> getMontorsByLevel(int level){
        List<MonitorBean> list = new ArrayList<>();
        for (MonitorBean bean : getAllMonitorList()) {
            if(MonitorUtils.getMonitorState(bean)== level){
                list.add(bean);
            }
        }
        return list;
    }


    /**
     * 依据区ID查找下面监测点预警级别
     * @param id 街道办ID
     * @return
     */
    public int getAreaState(int id){
        int state = 0;
        for (MonitorBean bean : getAllMonitorList()) {
            if(bean.getMonitorArea().getParentAreaId()==id){
                int level = MonitorUtils.getMonitorState(bean);
                if(state<MonitorUtils.getMonitorState(bean)){
                    state = level;
                }
            }
        }
        return state;
    }

    /**
     * 依据预警级别获取监测点列表
     * @return
     */
    public List<MonitorBean> getMontorByLevel(int level){
        List<MonitorBean> list = new ArrayList<>();
        for (MonitorBean bean : getAllMonitorList()) {
            if(MonitorUtils.getMonitorState(bean)== level){
                list.add(bean);
            }
        }
        return list;
    }

    /**
     * 按id查找区数据
     * @param id
     * @return
     */
    public DistrictBean getDistricById(int id){
        DistrictBean bean = null;
        for (DistrictBean districtBean : monitorBeanList) {
            if(id==districtBean.getDistrict().getDistrictId()){
                bean = districtBean;
                break;
            }
        }
        return bean;
    }


    /**
     * 获取红色级别监测点集合
     * @return
     */
    public List<MonitorBean> getRedLevelMontor(){
        List<MonitorBean> list = new ArrayList<>();
        for (MonitorBean bean : getAllMonitorList()) {
            if(MonitorUtils.getMonitorState(bean)== Constant.WARN_LEVEL_RED){
                list.add(bean);
            }
        }
        return list;
    }

    /**
     * 获取黄色级别监测点集合
     * @return
     */
    public List<MonitorBean> getYellowMontors(){
        List<MonitorBean> list = new ArrayList<>();
        for (MonitorBean bean : getAllMonitorList()) {
            if(MonitorUtils.getMonitorState(bean)== Constant.WARN_LEVEL_YELLOW){
                list.add(bean);
            }
        }
        return list;
    }

    /**
     * 获取黄色级别监测点集合
     * @return
     */
    public List<MonitorBean> getOrangeMontors(){
        List<MonitorBean> list = new ArrayList<>();
        for (MonitorBean bean : getAllMonitorList()) {
            if(MonitorUtils.getMonitorState(bean)== Constant.WARN_LEVEL_ORANGE){
                list.add(bean);
            }
        }
        return list;
    }

    /**
     * 按ID查找街道办数据
     * @param id
     * @return
     */
    public StreetBean getStreetById(int id){
        StreetBean bean = null;
        for (StreetBean streetBean : allStreetList) {
            if(id== streetBean.getRegionAreaId()){
                bean = streetBean;
                break;
            }
        }
        return bean;
    }


}
